package org.apix.commands;

import org.apix.Apix;
import org.apix.CooldownManager;
import org.bukkit.entity.Player;

public abstract class BaseCommand implements Command {
	
	protected Apix plugin;
	protected CooldownManager cooldownManager;
	
	private boolean enabled;
	private String permissionNode;
	private String commandName;
	
	
	public Command setPlugin(Apix plugin) {
		this.plugin = plugin;
		
		//this.util = plugin.getUtil();
		this.cooldownManager = plugin.getCooldownManager();
		enabled = true; // !plugin.getConfig().getBoolean(getDisabledConfigFlag(), Boolean.FALSE);
		return this;
	}

	
	public boolean isEnabled() {
		return enabled;
	}

	

	
	protected boolean defaultCommandChecks(Player p) {
		if( !enabled )
			return false;

		if(!hasPermission(p))
			return false;

		if( !cooldownCheck(p) )
			return false;

		return true;
	}

	/** Can be overridden, but default implementation just applies the command name
	 * as the lower case version of the class name of the implemented command.
	 */
	@Override
	public String getCommandName() {
		if( commandName == null ) {
			String className = this.getClass().getName();
			int index = className.lastIndexOf('.');
			commandName = className.substring(index+1).toLowerCase();
		}

		return commandName;
	}

	/** Here for convenience if the command has no aliases.  Otherwise, this should be overridden.
	 * 
	 */
	@Override
	public String[] getCommandAliases() {
		return null;
	}

	
	protected boolean cooldownCheck(Player p) {
		return cooldownManager.cooldownCheck(p, getCommandName());
	}

	
	protected boolean hasPermission(Player p) {		
			return true;
	}
}
